package com.zy01.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zy02.utils.PayConfig;
import com.zy02.utils.PaymentUtil;


//处理易宝支付成功通知
public class PaymentResponse extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//得到数据，并校验数据是否是易宝发送的
		String r0_Cmd = request.getParameter("r0_Cmd");
		String r1_Code = request.getParameter("r1_Code");
		String r2_TrxId = request.getParameter("r2_TrxId");
		String r3_Amt = request.getParameter("r3_Amt");
		String r4_Cur = request.getParameter("r4_Cur");
		String r5_Pid = request.getParameter("r5_Pid");
		String r6_Order = request.getParameter("r6_Order");
		String r7_Uid = request.getParameter("r7_Uid");
		String r8_MP = request.getParameter("r8_MP");
		String r9_BType = request.getParameter("r9_BType");
		String hmac = request.getParameter("hmac");
		
		String p1_MerId = PayConfig.getValue("p1_MerId");
		String keyValue = PayConfig.getValue("keyValue");
		boolean b = PaymentUtil.verifyCallback(hmac, p1_MerId, r0_Cmd, r1_Code, r2_TrxId, r3_Amt, r4_Cur, r5_Pid, r6_Order, r7_Uid, r8_MP, r9_BType, keyValue);
		
		if(!b){
			request.setAttribute("message", "交易签名被改动！！");
			request.getRequestDispatcher("/message.jsp").forward(request, response);
			return;
		}
		
		if("1".equals(r1_Code)){
			if(r9_BType.equals("1")){
				String message = "订单号：" + r6_Order + ",金额：" +  r3_Amt + "元,  已成功支付！！！！";
				request.setAttribute("message", message);
				request.getRequestDispatcher("/message.jsp").forward(request, response);
			}
			if(r9_BType.equals("2")){
				request.setAttribute("message", "success");
				request.getRequestDispatcher("/message.jsp").forward(request, response);
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
